package com.qiangqiang.task.server.service;

import com.qiangqiang.task.server.dto.MessageDTO;
import com.qiangqiang.task.server.dto.TaskMessageDTO;
import com.qiangqiang.task.server.entity.Message;
import com.qiangqiang.task.server.mapper.MessageMapper;
import com.qiangqiang.task.server.mapper.UserMapper;
import com.qiangqiang.task.server.util.TimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: task
 * @description:
 * @author: Sky.lin
 * @create: 2019-03-07 15:31
 **/
@Service
public class MessageService extends BaseService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private UserMapper userMapper;

    public List<MessageDTO> findPageMessageByToId(long toId) {
        List<Message> list = messageMapper.queryPageMessageByToId(toId);
        List<MessageDTO> res = new ArrayList<>();
        for (Message item : list) {
            // 当消息为 回复帖子时
            if (item.getType() == 1) {
                res.add(beanToDTO(item));
            } else {
                // 当消息为 领取或确认 任务时
                String name = userMapper.queryNickNameByUserId(item.getFromId());
                String time = TimeFormat.converTime(item.getCreateTime());
                TaskMessageDTO taskMessageDTO = new TaskMessageDTO();
                taskMessageDTO.setAchievement(userMapper.queryUserById(item.getFromId()).getAchievement());
                MessageDTO messageDTO = new MessageDTO(name, time, taskMessageDTO);
                messageDTO.setType(item.getType());
                res.add(messageDTO);
            }
        }
        return res;
    }
}
